package com.hwpt.hwaccount.feature.home;
import android.content.Context;

import com.hwpt.hwaccount.core.data.UserTableObserver;
import com.hwpt.hwaccount.core.data.local.entity.UserEntity;

import java.lang.ref.WeakReference;

public class HomePresenter implements HomeContract.Presenter {
    private WeakReference<HomeContract.View> viewRef;
    private final HomeContract.Model model;
    private final UserTableObserver userTableObserver;

    public HomePresenter(HomeContract.Model model, Context context) {
        this.model = model;
        this.userTableObserver = UserTableObserver.getInstance(context);
    }

    @Override
    public void attachView(HomeContract.View view) {
        this.viewRef = new WeakReference<>(view);
    }

    @Override
    public void detachView() {
        if (viewRef != null) {
            viewRef.clear();
        }
    }

    @Override
    public void onDestroy() {
        detachView();
    }

    @Override
    public void loadUserInfo() {
        HomeContract.View view = viewRef.get();

        model.loadUserInfo(new HomeContract.Model.LoadUserCallback() {
            @Override
            public void onSuccess(UserEntity user) {
                HomeContract.View view = viewRef.get();
                if (view != null) {
                    view.showUserInfo(user);
                }
            }

            @Override
            public void onError(String message) {
                HomeContract.View view = viewRef.get();
                if (view != null) {
                    view.showError(message);
                }
            }
        });
    }

    @Override
    public void onPersonalInfoClicked() {
        HomeContract.View view = viewRef.get();
        if (view != null) {
            view.navigateToPersonalInfo();
        }
    }

    @Override
    public void onAccountSecurityClicked() {
        HomeContract.View view = viewRef.get();
        if (view != null) {
            view.navigateToAccountSecurity();
        }
    }

    @Override
    public void onFeedbackClicked() {
        HomeContract.View view = viewRef.get();
        if (view != null) {
            view.navigateToFeedback();
        }
    }

    @Override
    public void onAboutClicked() {
        HomeContract.View view = viewRef.get();
        if (view != null) {
            view.navigateToAbout();
        }
    }

    @Override
    public void onLogoutClicked() {
        HomeContract.View view = viewRef.get();
        if (view != null && view.getContext() != null) {
            model.logout(new HomeContract.Model.LogoutCallback() {
                @Override
                public void onSuccess() {
                    HomeContract.View v = viewRef.get();
                    if (v != null) {
                        v.logoutSuccess();
                    }
                }

                @Override
                public void onError(String message) {
                    HomeContract.View v = viewRef.get();
                    if (v != null) {
                        v.showError(message);
                    }
                }
            }, view.getContext());
        } else {
            // 处理 Context 为空的情况
            HomeContract.View v = viewRef.get();
            if (v != null) {
                v.showError("无法获取上下文，退出登录失败");
            }
        }
    }

    /**
     * 停止全局观察
     */
    private void stopGlobalObservation() {
        if (userTableObserver != null && userTableObserver.isObserving()) {
            userTableObserver.stopObserving();
        }
    }
}